package com.yjt.sousou.camera.preview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.bumptech.glide.Glide;
import com.yjt.sousou.R;
import com.yjt.sousou.create.entity.ImageItem;

import java.util.ArrayList;
import java.util.List;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

/**
 * 作者：83734
 * 时间：2018/11/24
 * 描述：
 */
public class ImagePagerAdapter extends PagerAdapter {

    private List<ImageItem> mImageData = new ArrayList<>();
    private Context mContext;
    public PhotoViewClickListener listener;
    private int mFrom;
    public void setListener(PhotoViewClickListener listener) {
        this.listener = listener;
    }

    public void setFrom(int from) {
        mFrom = from;
    }

    public ImagePagerAdapter(Context context, List<ImageItem> mImageData) {
        this.mImageData = mImageData;
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return mImageData.size();
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_view_pager_image,null);
        PhotoView photoView = view.findViewById(R.id.photoview);
        ImageView imageView = view.findViewById(R.id.iv_play);
        final ProgressBar progressBar = view.findViewById(R.id.pb);
        final ImageItem item = mImageData.get(position);
        Glide.with(mContext)
                .asBitmap()
                .load(mFrom==0?item.getPath():item.getThumbUrl())
                .into(photoView);
        photoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
            @Override
            public void onPhotoTap(View view, float x, float y) {
                if (listener != null) listener.OnPhotoTapListener(view, x, y,item,progressBar);
            }
        });
        if(item.isVideo()){
            imageView.setVisibility(View.VISIBLE);
        }else {
            imageView.setVisibility(View.GONE);
        }
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view == o;
    }

    public interface PhotoViewClickListener {
        void OnPhotoTapListener(View view, float v, float v1, ImageItem item, ProgressBar progressBar);
    }
}

